<template>
  <div class="login">
    <div class="login_shadow">
      <div class="login_left">
        <div class="bg_custom">
          <div class="welcome">
            <span>Welcome欢迎</span>
            <img src="../assets/images/login/left_arrow.png" alt="left_arrow"/>
          </div>
          <div class="login_please">
            <span><span>请先</span>{{register ? '注册' : (resetPassword ? '重置密码' : '登录')}}</span>
          </div>
        </div>
      </div>
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
        <el-button v-show="register || resetPassword" type="text" icon="el-icon-close" class="close_btn" @click="goBack()"></el-button>
        <div class="items_container">
          <div class="header_container">
            <img v-show="!resetPassword && !register" src="../assets/logo/logo.png" alt="logo" class="logo"/>
            <div class="title">{{register ? '注册' : (resetPassword ? '重置密码' : '山地果园土壤-机械综合性能测试平台')}} </div>
          </div>
          <el-form-item prop="username">
            <el-input class="underline" v-model="loginForm.username" type="text" auto-complete="off" placeholder="请输入账号">
              <img slot="prefix" src="../assets/images/login/user_icon.png" alt="user_icon" class="input-icon"/>
              <!--          <svg-icon slot="prefix" icon-class="user"  />-->
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              class="underline"
              v-model="loginForm.password"
              type="password"
              auto-complete="off"
              placeholder="请输入密码"
            >
              <img slot="prefix" src="../assets/images/login/password_icon.png" alt="password_icon" class="input-icon"/>
              <!--          <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />-->
            </el-input>
          </el-form-item>
          <el-form-item v-show="resetPassword || register" prop="confirmPassword">
            <el-input
              class="underline"
              v-model="loginForm.confirmPassword"
              type="password"
              auto-complete="off"
              placeholder="请再次输入密码"
            >
              <img slot="prefix" src="../assets/images/login/password_icon.png" alt="password_icon" class="input-icon"/>
              <!--          <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />-->
            </el-input>
          </el-form-item>
          <el-form-item prop="code" v-if="captchaOnOff">
            <el-input
              class="underline"
              v-model="loginForm.code"
              auto-complete="off"
              placeholder="验证码"
              @keyup.enter.native="handleLogin"
            >
              <svg-icon style="width: 28px;height: 28px;margin-left: 0px;color: #18bb95" slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
              <div class="login-code" slot="suffix">
                <img :src="codeUrl" @click="getCode" class="login-code-img"/>
              </div>
            </el-input>
          </el-form-item>
          <div class="password_tool" v-show="!register && !resetPassword">
            <el-checkbox v-model="loginForm.rememberMe">记住密码</el-checkbox>
            <span class="forget_password pointer" @click="toResetPassword">忘记密码？</span>
          </div>
          <el-form-item style="width:100%;">
            <el-button
              round
              :loading="loading"
              type="primary"
              class="linerBtn"
              style="width:100%;"
              @click.native.prevent="handleAction"
            >
              <span v-if="!loading" style="font-size: 20px">{{register ? '注 册' : (resetPassword ? '重置密码' : '登 录')}}</span>
              <span v-else style="font-size: 20px">{{register ? '注 册 中...' : (resetPassword ? '重 置 中...' : '登 录 中...')}}</span>
            </el-button>
          </el-form-item>
          <p class="register_info" v-show="!register && !resetPassword">还没有账号？<span class="pointer to_register" @click="toRegister">马上注册</span></p>
        </div>
      </el-form>
    </div>
    <!--  底部  -->
    <div class="el-login-footer">
      <span>Copyright © 2018-2021 ruoyi.vip All Rights Reserved.</span>
    </div>
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from '@/utils/jsencrypt'
import Register from "./register";

export default {
  name: "Login",
  components: {Register},
  data() {
    return {
      codeUrl: "",
      cookiePassword: "",
      loginForm: {
        username: "display",
        password: "123456",
        confirmPassword: '',
        rememberMe: false,
        code: "",
        uuid: ""
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      // 验证码开关
      captchaOnOff: true,
      // 注册开关
      register: false,
      redirect: undefined,
      resetPassword: false,
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    this.getCode();
    this.getCookie();
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.captchaOnOff = res.captchaOnOff === undefined ? true : res.captchaOnOff;
        if (this.captchaOnOff) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.loginForm.uuid = res.uuid;
        }
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      };
    },
    handleAction() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove('rememberMe');
          }
          this.$store.dispatch("Login", this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
          }).catch(() => {
            this.loading = false;
            if (this.captchaOnOff) {
              this.getCode();
            }
          });
        }
      });
    },
    goBack() {
      this.resetPassword = false;
      this.register =  false;
      this.$refs.loginForm.resetFields();
    },
    toRegister() {
      this.register = true;
      this.loginForm = {
        username: '',
        password: '',
        confirmPassword: '',
        uuid: this.loginForm.uuid,
        code: ''
      }
      this.$refs.loginForm.resetFields();
    },
    toResetPassword() {
      this.resetPassword = true;
      this.loginForm = {
        username: '',
        password: '',
        confirmPassword: '',
        uuid: this.loginForm.uuid,
        code: ''
      }
      this.$refs.loginForm.resetFields();
    },
    handleRegister() {

    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("../assets/images/login/bg.png");
  background-size: cover;
  .login_shadow {
    box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.3);
    border-radius: 12px;
    display: flex;
  }
  .login_left {
    width: 400px;
    height: 640px;
    margin-right: -10px;
    background-image: url("../assets/images/login/left_bg.png");
    background-size: contain;
    background-repeat: no-repeat;
    background-position: center;
  }
  .bg_custom {
    width: 400px;
    height: 640px;
    padding: 180px 60px;
    background-color: rgba(12, 179, 124, 0.3);
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    .welcome {
      color: white;
      font-size: 20px;
      display: flex;
      flex-direction: row;
      justify-items: center;
      border-bottom: 3px white solid;
      padding-bottom: 10px;
      img {
        width: 28px;
        height: 28px;
        margin-left: 20px;
      }
    }
    .login_please {
      color: white;
      font-size: 28px;
      letter-spacing: 2px;
      span {
        span {
          border-bottom: 6px white solid;
          padding-bottom: 14px;
        }
      }
    }
  }
}
.title {
  margin: 0px auto 30px auto;
  text-align: left;
  color: #0CB37C;
  font-size: 32px;
  font-weight: 500;
  line-height: 40px;
}

.login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 600px;
  height: 640px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  z-index: 1;
  position: relative;
  .close_btn {
    position: absolute;
    top: 2px;
    right: 14px;
    i {
      font-size: 22px;
      font-weight: 600;
    }
  }
  .items_container {
    width: 400px;
  }
  .header_container {
    display: flex;
    flex-direction: row;
    .logo {
      width: 80px;
      height: 80px;
      margin-right: 12px;
    }
  }
  .el-form-item {
    margin-bottom: 16px;
  }
  .el-input {
    height: 56px;
    input {
      height: 56px;
    }
  }
  .input-icon {
    height: 30px;
    width: 30px;
    margin-left: -2px;
  }
  .el-input__inner {
    font-size: 16px;
    padding-left: 50px;
  }
  .el-input__prefix {
    margin-top: 12px
  }
  .linerBtn {
    background: linear-gradient(90deg, #22A894 0%, #04B773 100%);
    height: 46px;
    border-radius: 23px;
    margin-top: 30px;
  }
  .password_tool {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    margin-top: 30px;
    padding-left: 8px;
    padding-right: 4px;
    .el-checkbox {
      color: #c0c4cc;
    }
    .forget_password {
      color: #c0c4cc;
      font-size: 14px;
    }
  }
  .register_info {
    font-size: 14px;
    color: #c0c4cc;
    text-align: center;
    margin-top: 40px;
    .to_register {
      color: #0CB37C;
    }
  }
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  height: 38px;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-size: 12px;
  letter-spacing: 1px;
}
.login-code-img {
  height: 38px;
}
</style>
